package com.itita.weibo.ui;


import java.util.List;

import weibo4andriod.Status;
import weibo4andriod.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.manager.WeiboManager;
import com.itita.weibo.model.AccountManagerAdapter;
import com.itita.weibo.model.StatusAdapter;
import com.itita.weibo.util.WeiboUtil;
/**
 * 账号管理
 * @author 焦明 2010-5-10 21:30
 *
 */
public final class AccountManagerActivity extends Activity implements IWeiboActivity{
	
	private ListView listView;
	
	private List<User> userCountList;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WeiboUtil.closeWindowTitle(this);
        setContentView(R.layout.accountmanager);
        init();
        this.initControlsAndAddControlsEvent();
        MainService.addActivity(this);
    }
   

	@Override
	public void initControlsAndAddControlsEvent() {
		listView=(ListView)this.findViewById(R.id.lvAccountManager);
		
		
		listView.setAdapter(new AccountManagerAdapter(this, userCountList)); 
		//获取标题
    	final View titleview=this.findViewById(R.id.title4AccountManager);
    	
    	final TextView tv=(TextView)titleview.findViewById(R.id.textView);
    	
//    	final ImageView imageViewTitle = (ImageView) titleview.findViewById(R.id.imageViewTitle);
//    	imageViewTitle.setImageResource(R.drawable.acccount);
    	
    	 tv.setText(R.string.accountMana);
    	
    	final Button btnRightbolg=(Button)titleview.findViewById(R.id.title_bt_right);
    	final Button btnLeft=(Button)titleview.findViewById(R.id.title_bt_left);
	   
	    btnRightbolg.setText("编辑");
	    
	    btnLeft.setBackgroundResource(R.drawable.title_back);
	    
//	    btnLeft.setText("返回");
	    btnLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
                    finish();				
			}
		});
	    btnRightbolg.setOnClickListener(new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		for(int i=0, length = userCountList.size(); i<length; i++){
	    			View convertView =  listView.getChildAt(i);
	    			Button btnDelete = (Button) convertView.findViewById(R.id.btnAccountDelete);
	    			btnDelete.setVisibility(View.VISIBLE);
	    			ImageView ivTrianle = (ImageView) convertView.findViewById(R.id.ivTrianle);
	    			ivTrianle.setVisibility(View.GONE); 
	    		}
	    		
	    	}
	    });
	    
	    
	    //对ListView进行事件处理
	    listView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
//				if(arg3==-1){  //更多
//				   Intent intent = new Intent(AccountManagerActivity.this, AddCountctivity.class);
//				   startActivityForResult(intent, 1);
//				}else{
//					 
//					 //登陆
//				}
				 Intent intent = new Intent(AccountManagerActivity.this, AddCountctivity.class);
				 if(arg3 != -1){
					 Bundle bundle = new Bundle();
					 AccountManagerAdapter accountManagerAdapter=(AccountManagerAdapter)listView.getAdapter();
					 User user = (User)accountManagerAdapter.list.get(arg2);
					 intent.putExtra("user",user  );
				 }
				 startActivityForResult(intent, 1);
			}
	    	
	    });
//	    
		
	}
	
	@Override
	public void init() {
		this.userCountList = new WeiboManager().searchUser(this); 
	}

	@Override
	public void refresh(Object... param) {
		if(param[1] != null){
			WeiboUtil.myToastShow(this, (String)param[1]);
//			for(int i=0, length = userCountList.size(); i<length; i++){
//    			View convertView =  listView.getChildAt(i);
//    			Button btnDelete = (Button) convertView.findViewById(R.id.btnAccountDelete);
//    			btnDelete.setVisibility(View.GONE);
//    			ImageView ivTrianle = (ImageView) convertView.findViewById(R.id.ivTrianle);
//    			ivTrianle.setVisibility(View.GONE); 
//    		}dd  
			init();
//			ffnotifyDataSetChanged   
			this.listView.setAdapter(new AccountManagerAdapter(this, userCountList));
			//((AccountManagerAdapter)this.listView.getAdapter()).notifyDataSetChanged();
		 
		}else{
			WeiboUtil.myToastShow(this, "删除账号失败");
		}
	}

}